package com.aiddea.maps;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class HelloGoogleMaps extends MapActivity {
    /** Called when the activity is first created. */
	
	MapView mapView;
	MapController mapCtrl;
	int curr_latM = 8000000;
	int curr_lngM = -81000000;
	int curr_lat = 8;
	int curr_lng = -81;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        init();
        
        setEvents();
        
        proximitat();
        
    }
 
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private void init()
	{
		// Map View
        mapView = (MapView) findViewById(R.id.mapview);
        /**/
        // Mostra els controls de ZOOM
        mapView.setBuiltInZoomControls(true);
        //mapView.setClickable(true);
        
        // Controlador que ens servirà per manipular la vista del mapa
        mapCtrl = mapView.getController();
        
        updateLatLng(curr_latM,curr_lngM);
        
	}
	
	private void updateLatLng(int LatE6, int LngE6)
	{
		curr_lngM = LatE6;
		curr_latM = LngE6;
		curr_lng = ((int) ((curr_lngM-400000) / 1E6));
        curr_lat = ((int) ((curr_latM+300000) / 1E6));
        
        Toast.makeText(getApplicationContext(), ""+curr_lngM+" | "+curr_latM+"\n"+curr_lng+" | "+curr_lat, 1000).show();
        
	}
	
	private void setEvents()
	{
		
		MapView mp = (MapView) findViewById(R.id.mapview);
		
		
		mp.setOnTouchListener(new OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				
				boolean bres = true;
				
				if(event.getAction() == MotionEvent.ACTION_DOWN)
				{
					final GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
					
					updateLatLng(p.getLatitudeE6(),p.getLongitudeE6());

					bres = true;
					
				}
				
				if(event.getAction() == MotionEvent.ACTION_UP)
				{
					proximitat();
					bres = true;
				}
				
				return bres;
			}
		});
		
		
	}
	
	private void proximitat()
	{
		        
        List<Overlay> mapOverlays = mapView.getOverlays();
        mapOverlays.clear();
        
        // Icona que s'utilitzarà com Marker
        Drawable drawable = this.getResources().getDrawable(R.drawable.bluemarkera);
        HelloItemizedOverlay itemsOverLay = new HelloItemizedOverlay(drawable, getApplicationContext());
        
        // Recuperem la info segons les coords que li passem
        // S'han de passar el idioma i el usuari
        GeoWikiInfo oGWI = new GeoWikiInfo(curr_lat, curr_lng, getApplicationContext(), "en", "wakumaku", 20.0, 10);
                
        // Afegim les dades recuperades a la colecció de Overlays
		for (GWI_Element GWList : oGWI.getGWI_Items()) {			
			itemsOverLay.addGWI_Element(GWList);
		}
		
		if(itemsOverLay.size()>0)
		{
			
			Toast.makeText(getApplicationContext(), "S'han trobat "+ (oGWI.getGWI_Items().size()) +" llocs a " + oGWI.getRadius() + "Km a la rodona...", Toast.LENGTH_LONG).show();
			
			// Test: Mostra una torradeta amb la info recuperada
	        // oGWI.showInfo();
	        		
			// Assignem l'array d'overlays al mapa
	        mapOverlays.add(itemsOverLay);
	        
	        // Establim el centre del mapa
	        //mapCtrl.animateTo(new GeoPoint(curr_latM, curr_lngM));
	        mapCtrl.animateTo(itemsOverLay.getCenter());
	        
	        // Establim el ZOOM: 1 - 15  
	        mapCtrl.setZoom(5);  
	        
		}else
		{
			Toast.makeText(getApplicationContext(), "No s'ha trobat res per aqui...", Toast.LENGTH_LONG).show();
		}
		
	}
	
	
}